const db = require('../db/dbconfig');

// 获取所有游戏类型
const getgametypes = async(req, res) => {
    let { keyword, pagenum, pagesize } = req.query;
    let sql = 'select * from gametype',
        data = null,
        msg = '请求成功！',
        status = 200,
        total = 0
    let count_sql = `select count(*) 'total' from gametype`
    try {
        if (pagenum && pagesize) {
            if (keyword) {
                sql += ` where typename like '%${keyword}%'`
                count_sql += ` where typename like '%${keyword}%'`
            }
            sql += ` limit ${(pagenum-1) * pagesize},${pagesize}`
            data = await db.sqlconnection(sql)
            const count_result = await db.sqlconnection(count_sql)
            total = count_result[0].total
        } else {
            status = 400
            msg = '参数必填'
        }
    } catch (error) {
        msg = `请求发生错误:${error}`
        status = 400
    } finally {
        res.status(status).send({
            data,
            total,
            meta: {
                status,
                msg
            }
        })
    }
}

// 添加游戏类型
const addgametype = async(req, res) => {
    let { typename, description } = req.body,
        status = 200
    insert_sql = `insert into gametype (typename,description) values (?,?)`,
        msg = '添加成功',
        data = null
    if (typename) { // 判断输入是否为空
        let select_sql = `select * from gametype where typename = '${typename}'`
        const result = await db.sqlconnection(select_sql);
        if (result.length === 0) { // 判断游戏类型是否已经存在
            let add_result = await db.sqlconnection(insert_sql, [typename, description ? description : null])
            if (add_result.affectedRows <= 0) { // 判断新增sql语句是否添加成功
                msg = '游戏类型添加失败'
                status = 400
            }
        } else {
            status = 400
            msg = '游戏类型已经存在'
        }
    } else {
        status = 400
        msg = '参数必填'
    }
    res.status(status).send({
        data,
        msg
    })
}

// 删除用户信息
const deletegametype = async(req, res) => {
    let { id } = req.query,
        sql = `delete from gametype where id = '${id}'`,
        msg = '删除类型成功',
        status = 200;
    try {
        const result = await db.sqlconnection(sql)
        if (result.affectedRows <= 0) { // 根据受影响行数来判断是否删除成功
            status = 400
            msg = '删除游戏类型信息失败'
        }
    } catch (error) {
        status = 400
        msg = `执行出错`
    } finally {
        res.status(status).send({
            msg,
            status
        })
    }
}

// 修改用户信息
const updategametype = async(req, res) => {
    let { id, typename, description } = req.body,
        status = 200
    update_sql = `update gametype set typename = ?,description = ? where id = ?`,
        msg = '修改信息成功!'
    if (id && typename) {
        const result = await db.sqlconnection(update_sql, [typename, description ? description : null, id]);
        if (result.affectedRows <= 0) {
            status = 400
            msg = '类型修改失败!'
        }
    } else {
        status = 400
        msg = '参数必填'
    }
    res.status(status).send({
        status,
        msg
    })
}

// 查询单个游戏类型信息
const get_one_gametype = async(req, res) => {
    let { id } = req.query,
        status = 200
    let sql = `select * from gametype where id =${id}`,
        data = null,
        msg = '请求成功！'
    try {
        if (id && id > 0) {
            data = await db.sqlconnection(sql)
        } else {
            status = 400
            msg = '必填参数'
        }
    } catch (error) {
        msg = `请求出错了,原因:${error}`
        status = 400
    } finally {
        res.send({
            data,
            meta: {
                status,
                msg
            }
        })
    }
};

module.exports = {
    getgametypes,
    addgametype,
    deletegametype,
    updategametype,
    get_one_gametype
}